[#include "../../dbadminer/common/html.ftl" /]
[@html]
    [@head_dbadminer title="权限配置列表" css=["fw", "bootstrap-table", "bootstrap-table-editable", "ztree-ajax"] /]
    [@body]
        [#--主体内容--]
        [@main ]
            [@connectTreeLeft /]
            <div class="am-g am-radius fw-tables">
                <div data-am-widget="tabs" class="am-tabs am-tabs-default">
                    <ul class="am-tabs-nav am-cf">
                        <li class="am-active"><a href="[data-tab-panel-0]">分配用户权限</a></li>
                        <li><a href="[data-tab-panel-1]">配置审批流程</a></li>
                    </ul>
                    <div class="am-tabs-bd">
                        <div data-tab-panel-0 class="am-tab-panel am-active">
                            <div class="fw-table">
                                <input id="memberUserIds" type="hidden" name="memberUserIds" />
                                [#--表格主体内容--操作栏--]
                                [@mainHeader toolbar="scope-toolbar"]
                                [#--[@shiro.hasPermission name="system:role:add"]--]
                                    <button class="am-btn am-btn-success am-radius" id="addUser" onclick="authUser()">添加用户</button>
                                [#--[/@shiro.hasPermission]--]
                                [#--[@shiro.hasPermission name="system:role:remove"]--]
                                    <button class="am-btn am-btn-danger am-radius multiple am-disabled" onclick="cancelAuthUserAll()">批量取消授权</button>
                                [#--[/@shiro.hasPermission]--]
                                [/@mainHeader]
                                <table id="bootstrap-table-scope"></table>
                            </div>
                        </div>
                        <div data-tab-panel-1 class="am-tab-panel ">
                            <div class="fw-table">
                                [#--表格主体内容--操作栏--]
                                [@mainHeader toolbar="process-toolbar"]
                                [#--[@shiro.hasPermission name="monitor:operlog:remove"]--]
                                    <button class="am-btn am-btn-danger am-radius multiple am-disabled" onclick="$.operate.removeAll()">删除</button>
                                [#--[/@shiro.hasPermission]--]
                                [#--[@shiro.hasPermission name="monitor:operlog:remove"]--]
                                    <button class="am-btn am-btn-danger am-radius" onclick="$.operate.clean()">清空</button>
                                [#--[/@shiro.hasPermission]--]
                                [/@mainHeader]
                                <table id="bootstrap-table-process"></table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        [/@main]

        [#--PC/APP 右侧悬浮按钮--]
        [@rightToolbar /]

        [@script_dbadminer js=["fw", "bootstrap-table", "bootstrap-table-editable", "ztree-ajax"]]
            <script type="text/javascript" src="${ctx}/osfw/dbadminer/connect/connect-select.js"></script>
            <script type="text/javascript">
                var prefix = ctx + "osfw/dbadminer/config";
                [#--var editFlag = "${hasPermission('system:config:view')}";--]
                [#--var removeFlag = "${hasPermission('system:config:remove')}";--]
                var editFlag = "";
                var removeFlag ="";

                $(function () {
                    queryConnectTreeLeft();
                    queryScopeList();
                    queryProcessList();

                });

                function queryScopeList() {
                    var options = {
                        id: "bootstrap-table-scope",
                        url: prefix + "/scopeList",
                        modalName: "权限",
                        rememberSelected: true,
                        uniqueId: "scopeId",
                        toolbar: "scope-toolbar",
                        columns: [
                            {
                                // field: 'state',
                                checkbox: true
                            },
                            {
                                title: "序号",
                                formatter: function (value, row, index) {
                                    return $.table.serialNumber(index);
                                }
                            },
                            {
                                field: 'userId',
                                title: '姓名',
                                formatter: function (value, row, index) {
                                    // queryApproval
                                    return value == '1818113346193338369' ? '测试' : '测试2';
                                }
                            },
                            {
                                field: 'phonenumber',
                                title: '手机',
                                formatter: function (value, row, index) {
                                    // queryApproval
                                    return row.userId == '1818113346193338369' ? '13111111114' : '13111111113';
                                }
                            },
                            {
                                field: 'deptName',
                                title: '部门',
                                visible: false,
                            },
                            {
                                field: 'queryPermit',
                                title: '查询',
                                formatter: function (value, row, index) {
                                    // queryApproval
                                    return value
                                }
                            },
                            {
                                field: 'addPermit',
                                title: '添加',
                                formatter: function (value, row, index) {
                                    // addApproval
                                    return value
                                }
                            },
                            {
                                field: 'modifyPermit',
                                title: '修改',
                                formatter: function (value, row, index) {
                                    // modifyApproval
                                    return value
                                }
                            },
                            {
                                field: 'deletePermit',
                                title: '删除',
                                formatter: function (value, row, index) {
                                    // deleteApproval
                                    return value
                                }
                            },
                            {
                                field: 'importPermit',
                                title: '导入',
                                formatter: function (value, row, index) {
                                    // importApproval
                                    return value
                                }
                            },
                            {
                                field: 'exportPermit',
                                title: '导出',
                                formatter: function (value, row, index) {
                                    // exportApproval
                                    return value
                                }
                            },
                            {
                                field: 'otherPermit',
                                title: '其他',
                                formatter: function (value, row, index) {
                                    // otherApproval
                                    return value
                                }
                            },
                            {
                                title: '操作',
                                cellStyle: operCellStyle,
                                formatter: function(value, row, index) {
                                    var actions = [];
                                    actions.push('<a class="am-badge am-badge-transparent am-radius ' + removeFlag + '" href="javascript:void(0)" onclick="cancelAuthUser(\'' + row.userId + '\')">取消授权</a> ');
                                    return actions.join('');
                                }
                            }]
                    };
                    $.table.init(options);
                }

                function queryProcessList() {
                    var options = {
                        id: "bootstrap-table-process",
                        url: prefix + "/processList",
                        modalName: "流程",
                        rememberSelected: true,
                        uniqueId: "processId",
                        toolbar: "process-toolbar",
                        columns: [
                            {
                                // field: 'state',
                                checkbox: true
                            },
                            {
                                title: "序号",
                                formatter: function (value, row, index) {
                                    return $.table.serialNumber(index);
                                }
                            },
                            {
                                field: 'connectName',
                                title: '连接名称',
                                formatter: function (value, row, index) {
                                    // queryApproval
                                    return connect_name(index);
                                }
                            },
                            {
                                field: 'databaseName',
                                title: '数据库',
                                formatter: function (value, row, index) {
                                    // queryApproval
                                    return database_name(index);
                                }
                            },
                            {
                                field: 'datatableName',
                                title: '数据表/视图'
                            },
                            {
                                field: 'operType',
                                title: '操作类型'
                            },
                            {
                                field: 'procKey',
                                title: '审批流程KEY',
                                formatter: function (value, row, index) {
                                    // queryApproval
                                    return process_key(index);
                                }
                            },
                            {
                                title: '操作',
                                cellStyle: operCellStyle,
                                formatter: function (value, row, index) {
                                    var actions = [];
                                    actions.push('<a class="am-badge am-badge-transparent am-radius ' + editFlag + '" href="javascript:void(0)" onclick="configProcess(\'' + row.connectId + '\',\'' + row.databaseName + '\',\'' + row.datatableName + '\',\'' + row.operType + '\')">配置流程</a> ');
                                    actions.push('<a class="am-badge am-badge-transparent am-radius ' + removeFlag + '" href="javascript:void(0)" onclick="removeProcess(\'' + row.connectId + '\',\'' + row.databaseName + '\',\'' + row.datatableName + '\',\'' + row.operType + '\')">删除流程</a>');
                                    return actions.join('');
                                }
                            }]
                    };
                    $.table.init(options);
                }

                /* 人员选择回调 */
                function selectUsersCallback(elementId, userIds, loginNames, userNames){
                    console.log(elementId, userIds, loginNames, userNames);
                    if(elementId == 'memberUser'){
                        var userArr = userIds.split(",");
                        if(userArr.length > 60){
                            $.modal.alertWarning("抱歉，最多支持选择六十名人员");
                        }else {
                            $('#'+elementId+'Ids').val(userIds);
                        }
                    }else {
                        $('#'+elementId+'Id').val(userIds);
                    }

                    submitHandler();
                }

                /* 添加用户-选择用户-提交 */
                function submitHandler() {
                    table.set("bootstrap-table-scope");
                    var userIds = $('#memberUserIds').val();
                    if (userIds.length == 0) {
                        $.modal.alertWarning("请至少选择一条记录");
                        return;
                    }
                    var data = {
                        "connectId": $("input[name=connectId]").val(),
                        "databaseName": $("input[name=databaseName]").val(),
                        "datatableName": $("input[name=datatableName]").val(),
                        "userIds": userIds
                    };
                    $.operate.submit(prefix + "/authUsers", "post", "json",  data);
                }

                /* 分配用户-批量取消授权 */
                function cancelAuthUserAll(userId) {
                    var rows = $.common.isEmpty(table.options.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns(table.options.uniqueId);
                    if (rows.length == 0) {
                        $.modal.alertWarning("请至少选择一条记录");
                        return;
                    }
                    $.modal.confirm("确认要删除选中的" + rows.length + "条数据吗?", function() {
                        var data = { "roleId": $("#roleId").val(), "userIds": rows.join() };
                        $.operate.submit(prefix + "/cancelAll", "post", "json", data);
                    });
                }

                /* 分配用户-取消授权 */
                function cancelAuthUser(userId) {
                    $.modal.confirm("确认要取消该用户操作权限吗？", function() {
                        $.operate.post(prefix + "/cancel", { "roleId": $("#roleId").val(), "userId": userId });
                    });
                }


                /* 配置流程 */
                function configProcess(connectId, databaseName, datatableName, operType) {
                    var option = '<option value="" selected>-- 请选择 --</option>\n';
                    $.operate.get(ctx + "osfw/activiti/procdef" + "/selectProcdefByProcKey?prockey="+"23a93654225a", function (res) {
                        $.each(res.data, function (index, item) {
                            option += '<option value="'+item.version+'"> 版本号 [ V'+item.version+' ] --- 流程名称 [ '+item.procName+' ]</option>\n';
                        });
                        $.modal.prompt("确定配置该数据库的审批流程吗？", "请选择您要配置的流程：", "", "select", function (data) {
                        }, "cancel", option, "", "");
                    });
                }
                /* 删除流程 */
                function removeProcess() {
                    $.modal.confirm("确认要删除该流程配置吗?", function () {

                    });
                }

                function connect_name(index) {
                    var str = "pgsql本地库";
                    return str;
                }
                function database_name(index) {
                    var str = "fanwei_dbadminer";
                    return str;
                }
                function process_key(index) {
                    var str = "23a93654225a";
                    return str;
                }

                function authUser() {
                    $.modal.openFull("添加用户授权", ctx + "osfw/dbadminer/config/add");
                }

            </script>
        [/@script_dbadminer]
    [/@body]
[/@html]
